const app = getApp();
const promisify = require("../../utils/promisify")

Page({
  data: {
    name: undefined,
    price: undefined,
    brief: undefined,
    imgs: undefined,
    commentsCount: 0,
    comments: [],
    id: undefined,
    parentId: undefined,
    like: ""
  },
  onLoad: function (data) {
    let that = this
    let id = Number(data.id)
    let parentId = Number(data.parentId)
    let favorite = app.globalData.globalFavorite
    favorite.forEach((item, index) => {
      if (item.id === id && item.parentId === parentId) {
        that.setData({
          like: "like"
        })
      }
    })
    that.setData({
      id,
      parentId,
      favorite
    })
    promisify(wx.request)({
      url: `https://www.cumthing.world/cumthing/products/product` + app.ToParams({
        Id: id
      }),
      method: "GET",
      header: {
        'Content-Type': 'application/json' // 默认值
      }
    }).then(
      data => {
        data = data.data
        console.log(data)
        that.setData({
          name: data.name,
          price: data.price,
          brief: data.description,
          imgs: data.images,
          comments: data.comments,
          commentsCount: data.commentsCount
        })
      }
    )
  },
  onShow: function () {
    let that = this
    promisify(wx.request)({
      url: `https://www.cumthing.world/cumthing/product-info/collect` + app.ToParams({
        productId: that.data.id,
        userId: app.globalData.userId
      }),
      method: "GET",
      header: {
        'Authorization': app.globalData.token
      }
    }).then(
      data => {
        console.log(data.data.data)
        if (data.data.data.status === 1) {
          that.setData({
            like: "like"
          })
        } else if (data.data.data.status === 0) {
          that.setData({
            like: ""
          })
        }
      }
    )
  },
  addToCart: function () {
    /**
     * 添加到购物车
     * 如果还没有添加到购物车，则加入购物车，并提示已添加
     * 如果已经添加，则提醒购物车已经存在该物品
     */
    if (app.loginFirst()) {
      let that = this
      let globalCart = app.globalData.globalCart
      let parentId = that.data.parentId
      let id = that.data.id
      let flag = 0
      globalCart[parentId - 1].items.forEach((item, index) => {
        if (item.id === id) {
          flag = 1;
        }
      })
      if (flag === 0) {
        promisify(wx.request)({
          url: `https://www.cumthing.world/cumthing/product-cart/addOrDecrease` + app.ToParams({
            userId: app.globalData.userId,
            productId: that.data.id,
            count: 1
          }),
          method: 'POST',
          header: {
            'Content-Type': 'application/json',
            'Authorization': app.globalData.token
          }
        }).then(
          data => {
            console.log(data)
            if (data.data.msg === "成功") {
              app.globalData.globalCart[parentId - 1].items.push({
                id: that.data.id,
                num: 1,
                name: that.data.name,
                brief: that.data.brief,
                img: that.data.imgs[0].carouselImage,
                price: that.data.price,
                parentId: that.data.parentId,
                select: true,
                subStyle: "cansub",
                addStyle: "canadd"
              })
              // 存储到本地缓存
              promisify(wx.setStorage)({
                key: "globalCart",
                data: app.globalData.globalCart
              })
              wx.showToast({
                title: '已添加到购物车',
                icon: 'success',
                duration: 2000
              })
            }
          }
        )
      } else {
        wx.showToast({
          title: '物品已存在购物车',
          icon: 'none',
          duration: 2000
        })
      }
    }
  },
  preview: function (e) {
    let that = this
    let current = e.target.dataset.url
    let urls = []
    that.data.imgs.forEach((item, index) => {
      urls.push(item.carouselImage)
    })
    wx.previewImage({
      current: current,
      urls: urls
    })
  },
  like: function () {
    /**
     * 点击收藏的处理
     * 如果还没有收藏，则加入收藏夹
     * 如果已经收藏，则取消
     */
    let that = this
    let like = that.data.like
    if (app.loginFirst()) {
      promisify(wx.request)({
        url: `https://www.cumthing.world/cumthing/product-info/add/collect` + app.ToParams({
          productId: that.data.id,
          userId: app.globalData.userId
        }),
        method: "GET",
        header: {
          'Authorization': app.globalData.token
        }
      })
      if (like === "") {
        like = "like"
      } else {
        like = ""
      }
      that.setData({
        like
      })
    }
  }
})